package com.westar.base.util;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 页面特殊类型数据获取
 */
public class JsoupSelfUtil {

    /**
     * 取得单选框的值
     * @param ele 需要解析的单选项元素
     * @return
     */
    public static String getRadioVal(Elements ele){
        //默认单选框的值为空
        String radioVal = "";
        //便利单选选项
        for (Element radio :ele){
            //单选框被选中
            if(radio.hasAttr( "checked" )){
                //获取数据
                radioVal = radio.attr( "value" );
                //终止循环
                break;
            }
        }
        return radioVal;
    }

    /**
     * 取得多选框的值
     * @param ele 需要解析的多选项元素
     * @return
     */
    public static String getCheckBoxVal(Elements ele){
        //默认数值为空
        String radioVal = "";
        //遍历复选框选项
        for (Element radio :ele){
            //复选框被选中
            if(radio.hasAttr( "checked" )){
                //结果拼接
                radioVal += radio.attr( "value" )+",";
            }
        }
        return radioVal;

    }

    /**
     * 获取select的值
     * @param ele 下拉选项
     * @return
     */
    public static String getSelectVal(Elements ele) {
        //默认下拉选项为空
        String selectVal = "";
        //取得选中的元素
        Elements options = ele.select( "option[selected]" );
        //有选中的元素
        if(!options.isEmpty()){
            //获取数据
            selectVal = options.attr( "value" );
        }
        return selectVal;
    }
}
